% fname: routing_oddeven__sel_bufferlevel__topology_8x8__.m
% ./noxim -routing oddeven -sel bufferlevel -dimx 8 -dimy 8  -sim 10000 -warmup 2000 -size 8 8 -buffer 4 -hs 27 0.2 -hs 28 0.2 -hs 35 0.2 -hs 36 0.2 

function [max_pir, max_throughput, min_delay] = routing_oddeven__sel_bufferlevel__topology_8x8__(symbol)

data = [
%             pir      avg_delay     throughput      max_delay       rpackets         rflits
            0.002        18.9535      0.0161113            174           1032           8249
            0.002        17.7917      0.0160813             96           1013           8105
            0.002        17.5771      0.0156548             91            986           7890
            0.002        17.1594      0.0163711             92           1048           8382
            0.002        18.0879      0.0165021            133            990           7921
            0.002        21.4906      0.0171377            231           1011           8089
            0.002        17.3562      0.0152695            113            977           7818
            0.002        18.0763      0.0164738            211           1022           8171
            0.002        16.9428      0.0160437             99            961           7701
            0.002        20.4936      0.0174226            161           1100           8781
            0.002        16.9971      0.0165403            142           1025           8204
            0.002        17.4388      0.0173008            163           1021           8166
            0.002        16.0866      0.0160363             69            993           7954
            0.002        17.6345      0.0167208            119           1004           8026
            0.002        20.5879      0.0165377            264           1041           8335
            0.002        17.9961      0.0177091            105           1027           8217
            0.002        18.4451      0.0174738             93           1083           8667
            0.002        21.0325      0.0163528            264           1016           8111
            0.002        16.7803      0.0173012             82           1056           8443
            0.002        17.6293       0.017127             84           1079           8632
           0.0022        20.5429      0.0179663            134           1131           9055
           0.0022        19.5676       0.017754            124           1117           8948
           0.0022        22.6495      0.0181472            268           1124           9001
           0.0022        19.2306      0.0188369            103           1110           8891
           0.0022        19.8673      0.0176543            120           1130           9039
           0.0022        20.4667      0.0186492            145           1155           9250
           0.0022        20.1485      0.0173398            260           1111           8878
           0.0022        18.4809      0.0176094             92           1127           9016
           0.0022        21.9964      0.0174246            135           1097           8782
           0.0022        21.0859      0.0185806            121           1152           9216
           0.0022        19.5406      0.0188917            134           1134           9068
           0.0022        20.5826      0.0189274            143           1174           9388
           0.0022        21.8188      0.0181016            218           1159           9268
           0.0022        18.2633      0.0178581            111           1052           8429
           0.0022         17.149      0.0164688             86           1054           8432
           0.0022        19.3926      0.0175794            126           1108           8860
           0.0022          20.25      0.0186721            121           1140           9112
           0.0022        21.2044      0.0187083            201           1179           9429
           0.0022         20.867      0.0189073            166           1173           9378
           0.0022        18.3428      0.0171172            121           1094           8764
           0.0024        20.5109      0.0188671            120           1188           9509
           0.0024         23.055         0.0205            128           1292          10332
           0.0024         22.589      0.0202321            220           1275          10197
           0.0024        21.5693      0.0193611            160           1219           9758
           0.0024        23.5083      0.0190794            211           1202           9616
           0.0024        20.7801      0.0195887            126           1214           9716
           0.0024         23.086      0.0201914            198           1291          10338
           0.0024        20.7237       0.019625            208           1216           9734
           0.0024        22.2156       0.019418            265           1243           9942
           0.0024        22.7051      0.0197441            297           1265          10109
           0.0024        24.4928      0.0197222            180           1242           9940
           0.0024        19.9924      0.0184141            154           1178           9428
           0.0024        27.4606      0.0200766            313           1244           9958
           0.0024        21.2671      0.0206815            137           1284          10258
           0.0024        22.5816      0.0199816            140           1219           9751
           0.0024        24.2935      0.0189008            178           1189           9526
           0.0024        22.1817      0.0191074            166           1222           9783
           0.0024        22.9859      0.0188652            233           1208           9659
           0.0024        20.9804      0.0197641            165           1225           9803
           0.0024        25.1128      0.0191055            255           1223           9782
           0.0026        26.5762      0.0220143            199           1345          10743
           0.0026        24.1176      0.0207619            182           1309          10464
           0.0026         24.492      0.0205977            233           1317          10546
           0.0026        24.1859      0.0210794            180           1329          10624
           0.0026        21.9992      0.0211528            124           1331          10661
           0.0026        24.1399       0.020629            196           1301          10397
           0.0026         22.895      0.0211855            216           1314          10508
           0.0026         27.065      0.0206758            289           1324          10586
           0.0026        24.6014      0.0206706            252           1302          10418
           0.0026        23.8429      0.0212031            284           1356          10856
           0.0026        23.0143      0.0214254            168           1328          10627
           0.0026        23.8197      0.0199297            165           1276          10204
           0.0026        26.8286      0.0216172            181           1383          11068
           0.0026        24.6803      0.0213075            193           1342          10739
           0.0026        22.6964      0.0200801            157           1288          10281
           0.0026         23.994      0.0207949            237           1331          10647
           0.0026         21.496      0.0196797            109           1258          10076
           0.0026        22.3238      0.0207637            205           1328          10631
           0.0026         23.441      0.0218175            260           1374          10996
           0.0026        30.0128       0.021959            320           1406          11243
           0.0028        36.0475       0.022375            464           1433          11456
           0.0028        28.1756      0.0217871            272           1395          11155
           0.0028        26.0856      0.0229782            212           1449          11581
           0.0028        26.1727      0.0221582            258           1419          11345
           0.0028        28.4289      0.0221074            203           1413          11319
           0.0028        28.6628          0.023            316           1450          11592
           0.0028        29.4638       0.022459            330           1438          11499
           0.0028        28.4586      0.0222103            252           1400          11194
           0.0028        24.4059      0.0225957            166           1446          11569
           0.0028        24.4572      0.0210156            362           1345          10760
           0.0028        29.6369      0.0230742            362           1476          11814
           0.0028        36.2752      0.0231309            400           1479          11843
           0.0028        30.2236      0.0235488            260           1507          12057
           0.0028        23.6463      0.0216953            164           1388          11108
           0.0028         27.973      0.0219473            214           1405          11237
           0.0028        27.0772      0.0222656            222           1424          11400
           0.0028        26.9604      0.0225117            251           1441          11526
           0.0028        36.2739      0.0220893            528           1391          11133
           0.0028        22.8757      0.0221032            148           1392          11140
           0.0028        26.7491      0.0216621            234           1387          11091
            0.003        32.2454       0.023994            344           1512          12093
            0.003         30.007      0.0245527            234           1573          12571
            0.003        43.1751      0.0241875            556           1548          12384
            0.003        38.6548      0.0249425            676           1570          12571
            0.003        31.0419      0.0238594            197           1526          12216
            0.003        37.3749      0.0246647            459           1555          12431
            0.003        30.3145      0.0245262            319           1520          12165
            0.003        28.2262      0.0238516            216           1525          12212
            0.003        39.8256      0.0242897            340           1531          12242
            0.003        34.9554      0.0244824            441           1568          12535
            0.003        34.1441      0.0241816            523           1548          12381
            0.003        37.7201      0.0245794            514           1547          12388
            0.003        43.5219      0.0250317            641           1577          12616
            0.003        34.6065      0.0235488            632           1507          12057
            0.003        29.8347      0.0240873            302           1518          12140
            0.003        34.5467      0.0235723            344           1509          12069
            0.003        36.5768      0.0241191            428           1543          12349
            0.003        36.6145      0.0230391            671           1476          11796
            0.003        33.6336      0.0246528            335           1553          12425
            0.003        36.6055      0.0242559            439           1554          12419
           0.0032        38.7671      0.0259881            719           1636          13098
           0.0032        45.6768      0.0255734            697           1612          12889
           0.0032        46.4739      0.0257754            520           1648          13197
           0.0032        59.6929      0.0269093           1296           1667          13347
           0.0032         48.054      0.0260645            511           1668          13345
           0.0032        36.6882      0.0237656            416           1520          12168
           0.0032        43.5506      0.0261548            468           1649          13182
           0.0032         69.475      0.0258848           1005           1657          13253
           0.0032        41.4536      0.0263095            517           1658          13260
           0.0032        41.3725      0.0254121            530           1627          13011
           0.0032        44.5742      0.0262441            587           1679          13437
           0.0032        47.9561      0.0252871            451           1617          12947
           0.0032        39.5922      0.0250879            635           1606          12845
           0.0032        47.0679      0.0255586            519           1635          13086
           0.0032        49.4958      0.0257793            733           1650          13199
           0.0032        39.7837      0.0250254            434           1600          12813
           0.0032        73.3605      0.0270371            972           1728          13843
           0.0032        41.9697      0.0257539            552           1649          13186
           0.0032        39.2052      0.0251895            391           1613          12897
           0.0032        40.7898      0.0264758            343           1641          13132
           0.0034        97.2484      0.0284414           1455           1820          14562
           0.0034        69.8155      0.0264121            978           1691          13523
           0.0034        56.6023      0.0276984            537           1745          13960
           0.0034        74.6723      0.0268008            838           1715          13722
           0.0034        133.873      0.0276172           2247           1767          14140
           0.0034        44.2722      0.0269316            430           1723          13789
           0.0034        295.481      0.0276816           4552           1773          14173
           0.0034        67.8714      0.0279648            889           1789          14318
           0.0034        68.4553      0.0279141           1043           1788          14292
           0.0034        63.7153      0.0271016            919           1735          13876
           0.0034        57.9524      0.0266113            921           1703          13625
           0.0034        48.3552      0.0275417            688           1734          13881
           0.0034        59.3266      0.0282817            799           1782          14254
           0.0034        60.0763      0.0278889           1054           1757          14056
           0.0034        85.2627       0.028377           1049           1816          14529
           0.0034        62.8239      0.0270645            925           1732          13857
           0.0034        70.0503      0.0276426           1440           1769          14153
           0.0034        51.5128      0.0268496           1210           1716          13747
           0.0034        56.0364       0.027457            546           1756          14058
           0.0034        64.6877      0.0272227           1086           1742          13938
           0.0036        84.1771      0.0294008           1216           1852          14818
           0.0036        214.858      0.0290859           2387           1862          14892
           0.0036        77.1834       0.028168           1564           1805          14422
           0.0036        124.723      0.0291934           2471           1869          14947
           0.0036        131.996      0.0293869           1894           1852          14811
           0.0036        147.745      0.0287402           2277           1840          14715
           0.0036        84.7477      0.0276934           1010           1772          14179
           0.0036        212.934      0.0283281           3012           1813          14504
           0.0036        68.1505      0.0284648            760           1821          14574
           0.0036        75.8615      0.0292103           1033           1841          14722
           0.0036        130.739      0.0292676           1711           1873          14985
           0.0036         133.18      0.0290313           2532           1857          14864
           0.0036        61.1849      0.0280957            878           1801          14385
           0.0036        62.4269      0.0275547            611           1764          14108
           0.0036        67.6968       0.027959            633           1791          14315
           0.0036        90.2984      0.0282324           1307           1806          14455
           0.0036        52.8379       0.027881            935           1758          14052
           0.0036        117.282      0.0292871           1555           1874          14995
           0.0036        132.806      0.0280893           1996           1770          14157
           0.0036        59.4272      0.0271706           1003           1711          13694
           0.0038         201.18      0.0303867           2978           1946          15558
           0.0038        160.172      0.0301445           2565           1927          15434
           0.0038        92.6839      0.0302324           1467           1936          15479
           0.0038         144.62      0.0301032           1828           1896          15172
           0.0038          188.9      0.0302598           3164           1935          15493
           0.0038        180.643       0.030125           1804           1927          15424
           0.0038        219.581      0.0297559           3113           1904          15235
           0.0038        261.892       0.029748           5920           1903          15231
           0.0038         205.19      0.0291055           2886           1862          14902
           0.0038        134.637      0.0306389           1519           1929          15442
           0.0038        164.474      0.0301548           3414           1901          15198
           0.0038        171.053       0.029793           2466           1908          15254
           0.0038         201.82      0.0300332           2123           1921          15377
           0.0038        257.966      0.0305449           3940           1955          15639
           0.0038        138.358      0.0294336           2516           1884          15070
           0.0038        214.508       0.029707           2709           1901          15210
           0.0038        133.121      0.0297266           2303           1902          15220
           0.0038        96.0301      0.0290488           1523           1860          14873
           0.0038        130.872      0.0297871           2087           1907          15251
           0.0038        170.724      0.0290195           2542           1857          14858
];

rows = size(data, 1);
cols = size(data, 2);

data_delay = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+1);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_delay = [data_delay; data(ifirst, 1:cols-5), avg ci];
end

figure(1);
hold on;
plot(data_delay(:,1), data_delay(:,2), symbol);

data_throughput = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+2);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_throughput = [data_throughput; data(ifirst, 1:cols-5), avg ci];
end

figure(2);
hold on;
plot(data_throughput(:,1), data_throughput(:,2), symbol);

data_maxdelay = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+3);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_maxdelay = [data_maxdelay; data(ifirst, 1:cols-5), avg ci];
end

figure(3);
hold on;
plot(data_maxdelay(:,1), data_maxdelay(:,2), symbol);


%-------- Saturation Analysis -----------
slope=[];
for i=2:size(data_throughput,1),
    slope(i-1) = (data_throughput(i,2)-data_throughput(i-1,2))/(data_throughput(i,1)-data_throughput(i-1,1));
end

for i=2:size(slope,2),
    if slope(i) < (0.95*mean(slope(1:i)))
        max_pir = data_throughput(i, 1);
        max_throughput = data_throughput(i, 2);
        min_delay = data_delay(i, 2);
        break;
    end
end
